﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project.UserControls
{
    public partial class Spelers_UserControl : UserControl
    {
        private List<SingleSpeler_UserControl> panels;
        private Random random;

        private NieuwSpel_UserControl nuc;

        public Spelers_UserControl()
        {
            InitializeComponent();
            this.random = new Random();
            panels = new List<SingleSpeler_UserControl>();
            addPanel();
            addPanel();
            addPanel();
            addPanel();
        }

        public List<SingleSpeler_UserControl> Panels
        {
            get { return this.panels; }
            set { this.panels = value; }
        }

        public void addPanel()
        {
            SingleSpeler_UserControl panel = new SingleSpeler_UserControl("Speler " + (this.panels.Count() + 1), getRandomColor(), this);
            panels.Add(panel);
            updateLocations();
        }

        // Update Aantal Spelers
        public void updateAantalSpelers(int aantal)
        {
            if (aantal > panels.Count)
            {
                addPanel();
            }
            else if (aantal < panels.Count)
            {
                removePanel();
            }
        }

        // Verwijder een panel
        public void removePanel(SingleSpeler_UserControl panel = null)
        {
            if (panel != null)
            {
                panels.Remove(panel);
            }
            else
            {
                panels.RemoveAt(panels.Count - 1);
            }
            this.nuc.updateNumeric(this.panels.Count());
            updateLocations();
        }

        public void updateLocations()
        {
            this.Controls.Clear();
            for (int i = 0; i < panels.Count; i++)
            {
                panels[i].Parent = this;
                Point point;
                if (i % 2 == 0)
                {
                    point = new Point(0, 100 * (i / 2));
                }
                else
                {
                    point = new Point(200, 100 * (i / 2));
                }
                panels[i].Location = point;
            }
        }

        public Color getRandomColor()
        {
            return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
        }

        public NieuwSpel_UserControl Nuc
        {
            get { return nuc; }
            set { nuc = value; }
        }
    }
}
